package cn.interestingshop.dao;

import cn.interestingshop.entity.Classify;

import cn.interestingshop.entity.Goods;
import cn.interestingshop.utils.ClassifyVo;
import cn.interestingshop.utils.EmptyUtils;


import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClassifyVoDaoImpl extends BaseDaoImpl implements ClassifyVoDao {


    public ClassifyVoDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public List<ClassifyVo> createEntityByResultSet(ResultSet rs) throws Exception {

        List<ClassifyVo> classifyVoList1=new ArrayList<>();




        ResultSet rs2=null;
        ResultSet rs3=null;
        ResultSet rs4=null;

        ResultSet rs5=null;
        ResultSet rs6=null;
//        ResultSet rs7=null;



        String sql1="";
        String sql2="";
        String sql3="";

        String sql4="";
        String sql5="";
        String sql6="";





        while(rs.next()){


            List<Goods> goods=new ArrayList<>();
            ClassifyVo classifyVo1=new ClassifyVo();

            List<ClassifyVo> classifyVoList2=new ArrayList<>();


            Classify classify=new Classify();




            classify.setId(rs.getInt("a.id"));
            classify.setClassifyName(rs.getString("a.classify_name"));
            classify.setIcon(rs.getString("a.icon"));
            classify.setParentId(rs.getInt("a.parent_id"));
            classify.setType(rs.getInt("a.type"));

            sql1="select a.id,a.classify_name,a.parent_id,a.type,a.icon,b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level1_id  and a.id="+rs.getInt("a.id");
            rs2=executeQuery(sql1,null);
            sql4="select a.id,a.classify_name,a.parent_id,a.type,a.icon from t_classify a where a.parent_id="+rs.getInt("a.id");
            goodsList(rs2,goods);
            rs5=executeQuery(sql4,null);
            while (rs5.next()){

                List<ClassifyVo> classifyVoList3=new ArrayList<>();
                ClassifyVo classifyVo2=new ClassifyVo();
                Classify classify2=new Classify();

                classify2.setId(rs5.getInt("a.id"));
                classify2.setClassifyName(rs5.getString("a.classify_name"));
                classify2.setIcon(rs5.getString("a.icon"));
                classify2.setParentId(rs5.getInt("a.parent_id"));
                classify2.setType(rs5.getInt("a.type"));

                sql2="select a.id,a.classify_name,a.parent_id,a.type,a.icon,b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level2_id and a.parent_id="+rs5.getInt("a.id");
                rs3=executeQuery(sql2,null);
                goodsList(rs3,goods);
                sql5="select a.id,a.classify_name,a.parent_id,a.type,a.icon from t_classify a where  a.parent_id="+rs5.getInt("a.id");
                rs6=executeQuery(sql5,null);
                while(rs6.next()){

                    List<ClassifyVo> classifyVoList4=new ArrayList<>();

                    ClassifyVo classifyVo3=new ClassifyVo();
                    Classify classify3=new Classify();
                    classify3.setId(rs6.getInt("a.id"));
                    classify3.setClassifyName(rs6.getString("a.classify_name"));
                    classify3.setIcon(rs6.getString("a.icon"));
                    classify3.setParentId(rs6.getInt("a.parent_id"));
                    classify3.setType(rs6.getInt("a.type"));


                    sql3="select a.id,a.classify_name,a.parent_id,a.type,a.icon,b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level3_id and a.parent_id="+rs6.getInt("a.id");
                    rs4=executeQuery(sql3,null);
                    goodsList(rs4,goods);
//                    while (rs4.next()){
//                        ClassifyVo classifyVo4=new ClassifyVo();
//                        Classify classify4=new Classify();
//                        classify4.setId(rs4.getInt("a.id"));
//                        classify4.setClassifyName(rs4.getString("a.classify_name"));
//                        classify4.setIcon(rs4.getString("a.icon"));
//                        classify4.setParentId(rs4.getInt("a.parent_id"));
//                        classify4.setType(rs4.getInt("a.type"));
//
//                        classifyVo4.setClassify(classify4);
//
//                        classifyVoList4.add(classifyVo4);
//
//                    }

                    classifyVo3.setClassify(classify3);
//                    classifyVo3.setClassifyVoList(classifyVoList4);

                    classifyVoList3.add(classifyVo3);


                }

                classifyVo2.setClassify(classify2);
                classifyVo2.setClassifyVoList(classifyVoList3);

                classifyVoList2.add(classifyVo2);
            }



            classifyVo1.setGoodsList(goods);
            classifyVo1.setClassify(classify);
            classifyVo1.setClassifyVoList(classifyVoList2);


            classifyVoList1.add(classifyVo1);

        }




        closeResource();
        closeResource(rs2);
        closeResource(rs3);
        closeResource(rs4);
        closeResource(rs5);
        closeResource(rs6);








        return classifyVoList1;
    }

    public List<Classify> createEntityByResultSet2(ResultSet rs) throws Exception {


        List<Classify> list=new ArrayList<>();

        while (rs.next()) {

            Classify classify = new Classify();
            classify.setId(rs.getInt("a.id"));
            classify.setClassifyName(rs.getString("a.classify_name"));
            classify.setIcon(rs.getString("a.icon"));
            classify.setParentId(rs.getInt("a.parent_id"));
            classify.setType(rs.getInt("a.type"));

            try{
                if(!EmptyUtils.isEmpty(rs.getString("parentName"))){
                    classify.setParentName(rs.getString("parentName"));
                }

            }catch (Exception e){

            }finally {
                list.add(classify);
            }


        }

        return list;


    }


    public Classify createEntityByResultSet3(ResultSet rs) throws Exception {


        Classify classify = new Classify();

        while (rs.next()) {


            classify.setId(rs.getInt("a.id"));
            classify.setClassifyName(rs.getString("a.classify_name"));
            classify.setIcon(rs.getString("a.icon"));
            classify.setParentId(rs.getInt("a.parent_id"));
            classify.setType(rs.getInt("a.type"));


        }

        return classify;


    }


    public void goodsList(ResultSet rs,List<Goods>goodsList) throws Exception {





        while(rs.next()){





            Goods goods=new Goods();

            goods.setId(rs.getInt("b.id"));
            goods.setClassifyLevel1Id(rs.getInt("b.classify_level1_id"));
            goods.setClassifyLevel2Id(rs.getInt("b.classify_level2_id"));
            goods.setClassifyLevel3Id(rs.getInt("b.classify_level3_id"));
            goods.setFileName(rs.getString("b.file_name"));
            goods.setGoodsDesc(rs.getString("b.goods_desc"));
            goods.setGoodsName(rs.getString("b.goods_name"));
            goods.setPrice(rs.getFloat("b.price"));
            goods.setStock(rs.getInt("b.stock"));

            goodsList.add(goods);
        }



    }

    @Override
    public List<ClassifyVo> selectAll() {

        ResultSet rs=null;



        List<ClassifyVo> list1=new ArrayList<>();


        List<Goods> goods=new ArrayList<>();



        try{
            String sql="select a.id,a.classify_name,a.parent_id,a.type,a.icon from t_classify a where type=1";
            rs=executeQuery(sql,null);



            list1=createEntityByResultSet(rs);


            for (ClassifyVo classifyVo : list1) {
                for (ClassifyVo vo : classifyVo.getClassifyVoList()) {
                    System.out.println(vo.getClassify().getId());
                }
            }

//
//            classifyVo2.setClassifyVoList(list3);
//
//            classifyVo1.setGoodsList(goods);
//            classifyVo1.setClassifyVoList(list2);
//
//
//            list1.add(classifyVo1);


        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);

        }



        return list1;
    }

    @Override
    public List<Classify> selectAllon(Integer currentPageNo, Integer pageSize) {
        ResultSet rs=null;



       List<Classify> classifies=new ArrayList<>();



        try{
            String sql="select a.id,a.classify_name,a.parent_id,(select classify_name from t_classify where id=a.parent_id) as parentName,a.type,a.icon from t_classify a ";
            sql += " limit " + (currentPageNo - 1) * pageSize + "," + pageSize;
            rs=executeQuery(sql,null);




                classifies=createEntityByResultSet2(rs);







        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);

        }



        return classifies;
    }

    @Override
    public int selectAllCount() {
        ResultSet rs=null;



        int i=0;



        try{
            String sql="select count(1) from t_classify a";
            rs=executeQuery(sql,null);


            if(rs.next()){

                i=rs.getInt(1);
            }






        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);

        }



        return i;
    }

    @Override
    public int deleteById(int id) {
        int rs=0;

        try{
            String sql="delete from t_classify where id="+id;

            rs=executeUpdate(sql,null);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }



        return rs;
    }

    @Override
    public Classify selectOne(int id) {
        ResultSet rs=null;



        Classify classify=new Classify();



        try{
            String sql="select a.id,a.classify_name,a.parent_id,a.type,a.icon from t_classify a where id="+id;

            rs=executeQuery(sql,null);



            classify=createEntityByResultSet3(rs);








        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);

        }



        return classify;
    }

    @Override
    public List<Classify> selectOneType(int type) {
        ResultSet rs=null;





        List<Classify> classifies=new ArrayList<>();

        try{
            String sql="select a.id,a.classify_name,a.parent_id,a.type,a.icon from t_classify a where type="+type;

            rs=executeQuery(sql,null);




            classifies=createEntityByResultSet2(rs);







        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);

        }



        return classifies;
    }

    @Override
    public int update(int id, String classifyName, int classifyLevel1, int classifyLevel2) {

        int rs=0;

        String sql="";


        if(classifyLevel1==0 && classifyLevel2==0){
            sql="update t_classify set classify_name=? where id=?";
            Object[] objects=new Object[2];
            objects[0]=classifyName;
            objects[1]=id;

            rs=executeUpdate(sql,objects);

        }else if(classifyLevel1!=0 && classifyLevel2!=0){
            sql="update t_classify set classify_name=?,parent_id=? where id=?";
            Object[] objects=new Object[3];
            objects[0]=classifyName;
            objects[1]=classifyLevel2;
            objects[2]=id;

            rs=executeUpdate(sql,objects);

            sql="update t_classify set parent_id=? where id=?";
            Object[] objects2=new Object[2];
            objects2[0]=classifyLevel1;
            objects2[1]=classifyLevel2;

            rs=executeUpdate(sql,objects2);

        }else if(classifyLevel1!=0 && classifyLevel2==0){
            sql="update t_classify set classify_name=?,parent_id=? where id=?";
            Object[] objects=new Object[3];
            objects[0]=classifyName;
            objects[1]=classifyLevel1;
            objects[2]=id;

            rs=executeUpdate(sql,objects);

        }

        return rs;
    }

    @Override
    public int insert(int type, String classifyName, int classifyLevel1, int classifyLevel2) {
        int rs=0;

        String sql="";


        if(type==1){
            sql="insert into t_classify(classify_name,parent_id,type)values(?,?,?)";
            Object[] objects=new Object[3];
            objects[0]=classifyName;
            objects[1]=0;
            objects[2]=type;

            rs=executeUpdate(sql,objects);

        }else if(type==2){


            sql="insert into t_classify(classify_name,parent_id,type)values(?,?,?)";
            Object[] objects=new Object[3];
            objects[0]=classifyName;
            objects[1]=classifyLevel1;
            objects[2]=type;

            rs=executeUpdate(sql,objects);



        }else if(type==3){
            sql="insert into t_classify(classify_name,parent_id,type)values(?,?,?)";
            Object[] objects=new Object[3];
            objects[0]=classifyName;
            objects[1]=classifyLevel2;
            objects[2]=type;

            rs=executeUpdate(sql,objects);

            sql="update t_classify set parent_id=? where id=?";
            Object[] objects2=new Object[2];
            objects2[0]=classifyLevel1;
            objects2[1]=classifyLevel2;

            rs=executeUpdate(sql,objects2);

        }

        return rs;
    }

}


